package com.sjm.suchbear.minibookmall;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.minibookmall.Utils.T;
import com.sjm.suchbear.minibookmall.action.OrderManage;
import com.sjm.suchbear.minibookmall.action.UserManage;
import com.sjm.suchbear.minibookmall.model.OrderModel;
import com.sjm.suchbear.minibookmall.model.UserModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditInfoActivity extends Activity {

    @BindView(R.id.ll_header_back)
    LinearLayout llHeaderBack;
    @BindView(R.id.tv_list_title)
    TextView tvListTitle;
    @BindView(R.id.iv_header_function_ico)
    ImageView ivHeaderFunctionIco;
    @BindView(R.id.ll_header_function)
    LinearLayout llHeaderFunction;
    @BindView(R.id.ll_List_Main)
    RelativeLayout llListMain;
    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.tv_edit)
    TextView tvEdit;

    private String userID;
    private String orderID;
    private String realname;
    private String phone;
    private String address;
    private int mode; //0:修改用户信息   1:修改订单信息
    private SQLiteDatabase dbread;
    private UserModel userModel;
    private OrderModel orderModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_info);
        ButterKnife.bind(this);
        BookDB db = new BookDB(getBaseContext());
        dbread = db.getReadableDatabase();
        mode = getIntent().getIntExtra("mode", 0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mode == 0) {
            tvListTitle.setText("修改用户信息");
            userID = getIntent().getStringExtra("userID");
            userModel = UserManage.findUserByID(dbread, userID);
            etUsername.setText(userModel.getRealname());
            etPhone.setText(userModel.getPhone());
            etAddress.setText(userModel.getAddress());
        } else if (mode == 1) {
            tvListTitle.setText("修改订单信息");
            orderID = getIntent().getStringExtra("orderID");
            orderModel = OrderManage.getOrderListbyorderid(dbread, orderID);
            etUsername.setText(orderModel.getOrdername());
            etPhone.setText(orderModel.getOrderphone());
            etAddress.setText(orderModel.getOrderaddress());
        }
    }

    @OnClick({R.id.ll_header_back, R.id.tv_edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_header_back:
                finish();
                break;
            case R.id.tv_edit:
                if (mode == 0) {
                    changeUserInfo();
                } else if (mode == 1) {
                    changeOrderInfo();
                }
                break;
        }

    }

    private void changeUserInfo() {
        realname = String.valueOf(etUsername.getText());
        phone = String.valueOf(etPhone.getText());
        address = String.valueOf(etAddress.getText());
        if (realname.trim().equals("")) {
            T.showShort(getBaseContext(), "真实姓名不得为空");
            etUsername.requestFocus();
            return;
        }
        if (phone.trim().equals("")) {
            T.showShort(getBaseContext(), "手机号不得为空");
            etPhone.requestFocus();
            return;
        }
        if (address.trim().equals("")) {
            T.showShort(getBaseContext(), "收货地址不得为空");
            etAddress.requestFocus();
            return;
        }
        UserManage.changeUserInfo(dbread, userID, realname, phone, address);
        T.showShort(getBaseContext(), "修改成功");
        finish();
    }

    private void changeOrderInfo() {
        realname = String.valueOf(etUsername.getText());
        phone = String.valueOf(etPhone.getText());
        address = String.valueOf(etAddress.getText());
        if (realname.trim().equals("")) {
            T.showShort(getBaseContext(), "真实姓名不得为空");
            etUsername.requestFocus();
            return;
        }
        if (phone.trim().equals("")) {
            T.showShort(getBaseContext(), "手机号不得为空");
            etPhone.requestFocus();
            return;
        }
        if (address.trim().equals("")) {
            T.showShort(getBaseContext(), "收货地址不得为空");
            etAddress.requestFocus();
            return;
        }
        OrderManage.changeOrderInfo(dbread, orderID, realname, phone, address);
        T.showShort(getBaseContext(), "修改成功");
        finish();
    }
}
